import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:leader_doctor_flutter/common/utils/log_utils.dart';
import 'package:leader_doctor_flutter/model/event/event.dart';
import 'package:leader_doctor_flutter/styles/colors.dart';

import '../../../common/data/app.dart';
import '../../../common/utils/image_utils.dart';
import '../../../common/widget/comm_widget.dart';
import '../../../plugins/plugins_messages.g.dart';

/// @Author lixiaobin
/// @Date 2024/5/27
/// @Description
class MyInfoMainPage extends StatefulWidget {
  const MyInfoMainPage({super.key});

  @override
  State<MyInfoMainPage> createState() => _MyInfoMainPageState();
}

class _MyInfoMainPageState extends State<MyInfoMainPage> {
  String? _userAvatar;
  String? _userMale;

  @override
  void initState() {
    super.initState();
    _getUserAvatar();

    App.instance.eventBus.on<BaseEvent>().listen((event) {
      if ('set_avatar_succeed' == event.tag) {
        _getUserAvatar();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommWidget.getCommAppbar(context),
      backgroundColor: ColorRes.colorF9F9FA,
      body: Container(
          width: double.infinity,
          child: Column(
            children: [
              _buildItem('头像', () {
                NativeInterface().openNativePage('set_avatar', '').then((value) {
                  Log.d("打开发现回调");
                  _getUserAvatar();
                });
              }, isAvatarItem: true),
              CommWidget.buildLine(margin: EdgeInsets.only(left: 16.w), color: ColorRes.colorF2F2F2),
              _buildItem('个人介绍', () {
                NativeInterface().openNativePage('doctor_info', '');
              }),
              CommWidget.buildLine(margin: EdgeInsets.only(left: 16.w), color: ColorRes.colorF2F2F2),
              _buildItem('我的签名', () {
                NativeInterface().openNativePage('my_signature', '');
              }),
              CommWidget.buildLine(margin: EdgeInsets.only(left: 16.w), color: ColorRes.colorF2F2F2),
              _buildItem('认证信息', () {
                NativeInterface().openNativePage('auth_info', '');
              }),
            ],
          )),
    );
  }

  GestureDetector _buildItem(String str, Function onTop, {bool isAvatarItem = false}) {
    return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          onTop();
        },
        child: Container(
            height: 56.w,
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            color: ColorRes.white,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  str,
                  style: TextStyle(color: ColorRes.color333333, fontSize: 15.sp),
                ),
                Row(
                  children: [
                    Visibility(
                        visible: isAvatarItem,
                        child: ClipRRect(
                            borderRadius: BorderRadius.circular(25.w),
                            child: ImageLoader.loadSelfAvatar(_userAvatar,
                                sex: _userMale, size: Size(30.w, 30.w), fit: BoxFit.fill))),
                    SizedBox(width: 12.w),
                    ImageLoader.loadAsset('icon_my_info_more.webp', 0,size: Size(8.w, 15.w))
                  ],
                )
              ],
            )));
  }

  _getUserAvatar() {
    NativeInterface().getUserInfo().then((value) {
      var user = jsonDecode(value.userJson ?? '');
      _userAvatar = user?['headIcon'];
      _userMale = user?['userMedicalInfo']['sex'];
      setState(() {});
    });
  }
}
